<!--
 * @Description: 选择意见征询人及意见
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2022-10-14 16:18:21
 * @LastEditTime: 2022-11-15 16:40:06
 * @FilePath: \cscec-pms-admin\src\views\matters\approval\components\consultation\SelectConsultation.vue
-->
<template>
  <a-modal
    title="意见征询"
    :width="750"
    :visible="visible"
    :cancel-text="'关闭'"
    :ok-text="'提交'"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel()"
  >
    <custom-form
      ref="form"
      :is-validate="true"
      :edit-model="''"
      :rules="rules"
      :form="form"
      :form-list="formOption"
      class="requiredFormItem"
    />
  </a-modal>
</template>
<script>
import { formOption } from './model.js'
import { save } from '@/api/process'
import Vue from 'vue'
export default {
  name: 'SelectConsultation',
  components: {},
  props: {
    // 模态框显示隐藏
    visible: {
      type: Boolean,
      default: false
    },
    // 登录人信息
    info: {
      type: Object,
      default: () => {}
    },
    sendTaskId:{
      type:String,
      default:''
    }
  },
  data() {
    return {
      // 表单项
      // formOption,
      // 表单对象
      form: {},
      // 提交loading
      confirmLoading: false
    }
  },
  computed: {
    // 表单项
    formOption() {
      return formOption.map((item) => {
        item.disabled = this.detail
        return item
      })
    },
    // 表单校验
    rules() {
      const obj = {}
      this.formOption.map((item) => {
        if (item.rules) {
          obj[item.keyName] = item.rules
        }
      })
      return obj
    }
  },
  methods: {
    /**
     * @description: 确认
     * @return {void}
     */
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.confirmLoading = true
          const { id: userId, name: userName } = this.info
          const { opinions, receiveUserId } = this.form
          save({
            opinions,
            receiveUserId: receiveUserId.map((el) => el.key),
            taskId: !this.sendTaskId?this.$route.query.taskId:this.sendTaskId,
            userId,
            userName
          })
            .then((res) => {
              if (res.code === this.SUCCESS_CODE) {
                this.$message.success('提交成功')
                this.handleCancel(true)
              }
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    /**
     * @description: 取消
     * @param {boolean} refresh 是否刷新外部列表
     * @return {void}
     */
    handleCancel(refresh = false) {
      this.form = {}
      if (refresh) this.$emit('refresh', refresh)
      this.$emit('update:visible', false)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
